//定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。

typedef struct {
    int top;
    int *num; 
} stack;

typedef struct {
    stack A;    //数据栈
    stack B;    //辅助栈，非严格降序栈
} MinStack;
/** initialize your data structure here. */
void init_stack(stack *s) {
    s->top = -1;
    s->num = malloc(sizeof(int)*10000);
}
void push(stack *s, int val) {
    s->num[++s->top] = val;
}
int pop(stack *s) {
    return s->num[s->top--];
}
int top_val(const stack *s) {
    return s->num[s->top];
}

MinStack* minStackCreate() {
    MinStack* minstack = malloc(sizeof(MinStack));
    init_stack(&minstack->A);
    init_stack(&minstack->B);
    return minstack;
}


void minStackPush(MinStack* obj, int x) {
    push(&obj->A, x);   //A入栈
    if(obj->B.top == -1 || x <= top_val(&obj->B) ) push(&obj->B, x);  //如果B为空或x小于B的栈顶值，则B入栈
}

//注意保持A,B数据的一致性
void minStackPop(MinStack* obj) {
    int val = pop(&obj->A); //A出栈，记为值val
    if(val == top_val(&obj->B)) pop(&obj->B);   //如果A出栈值与B栈顶值相同，则B出栈
}

int minStackTop(MinStack* obj) {
    return top_val(&obj->A);    //取A的栈顶值
}

int minStackMin(MinStack* obj) {
    return top_val(&obj->B);    //取B的栈顶值
}

void minStackFree(MinStack* obj) {
    free(obj->A.num);
    free(obj->B.num);
    free(obj);
}

/**
 * Your MinStack struct will be instantiated and called as such:
 * MinStack* obj = minStackCreate();
 * minStackPush(obj, x);
 
 * minStackPop(obj);
 
 * int param_3 = minStackTop(obj);
 
 * int param_4 = minStackMin(obj);
 
 * minStackFree(obj);
*/